package com.dingwen.treasure.base.convert.jackson;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.fasterxml.jackson.databind.type.ClassKey;
import org.springframework.util.ObjectUtils;

/**
 * 　SimpleDeserializersWrapper
 * 　@author dingwen
 * 　@date 2022/6/29
 */
public class SimpleDeserializersWrapper extends SimpleDeserializers {
    @Override
    public JsonDeserializer<?> findEnumDeserializer(Class<?> type, DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException {

        // 没有自定义的枚举反序列化方式则采用默认的方式进行（数组下标）
        JsonDeserializer<?> defaultEumDeserializer = super.findEnumDeserializer(type, config, beanDesc);

        if (!ObjectUtils.isEmpty(defaultEumDeserializer)) {
            return defaultEumDeserializer;
        }
        // 查找对应的自定义序列化实现
        for (Class<?> typeInterface : type.getInterfaces()) {
            return this._classMappings.get(new ClassKey(typeInterface));
        }
        return null;
    }

}
